﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AAA.DataSource;
using AAA.TradeLanguage.Data;
using AAA.Meta.Quote.Data;

namespace AAA.TradeLanguage
{
    public class StrategyManager
    {
        private Dictionary<string, ISignal> _dicSignal;

        public StrategyManager()
        {
            _dicSignal = new Dictionary<string, ISignal>();
        }

        private PositionManager _positionManager;

        public PositionManager PositionManager
        {
            get { return _positionManager; }
            set 
            { 
                _positionManager = value;
                foreach (ISignal signal in _dicSignal.Values)
                {
                    signal.PositionManager = PositionManager;
                }
            }
        }

        private CurrentTime _currentTime;

        public CurrentTime CurrentTime
        {
            get { return _currentTime; }
            set 
            { 
                _currentTime = value;
                foreach (ISignal signal in _dicSignal.Values)
                {
                    signal.SetCurrentTime(_currentTime);
                }
            }
        }

        public void AddSignal(ISignal signal)
        {
            signal.PositionManager = PositionManager;
            if(_currentTime != null)
                signal.SetCurrentTime(_currentTime);
            if (_dicSignal.ContainsKey(signal.DisplayName))
            {
                _dicSignal[signal.DisplayName] = signal;
            }
            else
            {
                _dicSignal.Add(signal.DisplayName, signal);
            }
        }

        public void RemoveSignal(ISignal signal)
        {
            if(_dicSignal.ContainsKey(signal.DisplayName))
                return;

            _dicSignal.Remove(signal.DisplayName);

        }

        public List<SignalRecord> TradeRecord(string strSignalName)
        {
            if (_dicSignal.ContainsKey(strSignalName) == false)
                return null;

            return _dicSignal[strSignalName].TradeRecord();
        }

        public void VerifyClose()
        {
            foreach (ISignal signal in _dicSignal.Values)
            {
                signal.Calculate();
                signal.MatchPrice(signal.Close(signal.BaseSymbolId, 0));
            }
        }

        public void MatchPrice(float fPrice)
        {
            foreach (ISignal signal in _dicSignal.Values)
            {
                signal.Calculate();
                signal.MatchPrice(fPrice);
            }
        }

        public void PerformanceVarify(int iInterval, DateTime dtStartTime, DateTime dtEndTime)
        {
            int iOriginalInterval = _currentTime.TimeInterval;
            bool isContinue;

            try
            {                
                
                _currentTime.TimeInterval = iInterval;
                _currentTime.CurrentDateTime = dtStartTime;
                _currentTime.Reset();
                _positionManager.Reset();

                isContinue = true;

                foreach (ISignal signal in _dicSignal.Values)
                {
                    signal.ResetPerformance();
                }

                while (isContinue)
                {
                    foreach (ISignal signal in _dicSignal.Values)
                    {
                        signal.Calculate();
                        signal.VerifyPrice(VerifyPathEnum.Open_High_Low_Close);
                    }

                    _currentTime.MoveNext();
                    if (_currentTime.CurrentDateTime.CompareTo(dtEndTime) > 0)
                        isContinue = false;
                }

            }
            catch (Exception ex)
            {
                //throw new Exception("StrategyManager", ex);
                throw ex;
            }
            _currentTime.TimeInterval = iOriginalInterval;
        }

    }
}
